import React, { useEffect } from 'react';

const MyPromise = require('./myPromise');

function append(content, container = 'content', tagname = 'p') {
  const tag = document.createElement(tagname);
  const text = document.createTextNode(content);
  tag.appendChild(text);
  document.getElementById(container).appendChild(tag);
}

function main() {
  function p1() {
    return new MyPromise((resolve, reject) => {
      reject(new Error('p1 error'));
    });
  }
  p1()
    .then(value => {
      append(value);
    })
    .catch(reason => {
      append(reason.message);
    });
}

export default function CoreLogic() {
  useEffect(() => {
    main();
  }, [main]);

  return (
    <div id="content">
      <p>Promise 核心实现逻辑</p>
    </div>
  );
}
